using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace UnityHotFixEditor
{
    public static class AssetBundleTool
    {
        private static string saveFolder = null;
        private static void BuildAssetBundle(BuildTarget buildTarget)
        {
            if (saveFolder == null || saveFolder.Length == 0 || !Directory.Exists(saveFolder))
            {
                //saveFolder = Application.dataPath;
                saveFolder = Directory.GetCurrentDirectory();
            }
            if (Selection.count > 1)
            {
                string savePath = EditorUtility.OpenFolderPanel("选择保存文件夹", saveFolder, "");
                if (savePath == null || savePath.Length == 0)
                {
                    return;
                }
                saveFolder = savePath;
                List<AssetBundleBuild> assetBundleBuilds = new List<AssetBundleBuild>();
                foreach (var item in Selection.objects)
                {
                    AssetBundleBuild assetBundleBuild = new AssetBundleBuild();
                    assetBundleBuild.assetBundleName = item.name.ToLower() + ".temp";
                    assetBundleBuild.assetNames = new string[] { AssetDatabase.GetAssetPath(item) };
                    assetBundleBuilds.Add(assetBundleBuild);
                }
                // 第一个参数为打包文件的输出路径，第二个参数为打包资源的列表，第三个参数为打包需要的操作，第四个为打包的输出的平台
                AssetBundleManifest m = BuildPipeline.BuildAssetBundles(
                    saveFolder,
                    assetBundleBuilds.ToArray(),
                    BuildAssetBundleOptions.StrictMode,
                    buildTarget);
                foreach (var item in Selection.objects)
                {
                    string p = saveFolder + "/" + item.name;
                    if (File.Exists(p))
                    {
                        File.Delete(p);
                    }
                    File.Move(saveFolder + "/" + item.name.ToLower() + ".temp", p);
                }
            }
            else
            {
                string savePath = EditorUtility.SaveFilePanel("保存文件", saveFolder, Selection.activeObject.name, "");
                if (savePath == null || savePath.Length == 0)
                {
                    return;
                }
                saveFolder = Path.GetDirectoryName(savePath);
                string saveName = Path.GetFileName(savePath);
                // 第一个参数为打包文件的输出路径，第二个参数为打包资源的列表，第三个参数为打包需要的操作，第四个为打包的输出的平台
                AssetBundleManifest m = BuildPipeline.BuildAssetBundles(
                    saveFolder,
                    new AssetBundleBuild[]
                    {
                        new AssetBundleBuild
                        {
                            assetBundleName = saveName.ToLower() + ".temp",
                            assetNames = new string[] { AssetDatabase.GetAssetPath(Selection.activeObject) }
                        }
                    },
                    BuildAssetBundleOptions.StrictMode,
                    buildTarget);
                if (File.Exists(savePath))
                {
                    File.Delete(savePath);
                }
                File.Move(saveFolder + "/" + saveName.ToLower() + ".temp", savePath);
            }
            // 打开保存的文件夹
            System.Diagnostics.Process.Start(saveFolder);
        }
        [MenuItem("Assets/AssetBundle/打包资源")]
        public static void BuildAssetBundle()
        {
            BuildAssetBundle(EditorUserBuildSettings.activeBuildTarget);
        }
        [MenuItem("Assets/AssetBundle/打包资源Windows")]
        public static void BuildAssetBundleForWindows64()
        {
            BuildAssetBundle(BuildTarget.StandaloneWindows64);
        }
        [MenuItem("Assets/AssetBundle/打包资源Android")]
        public static void BuildAssetBundleForAndroid()
        {
            BuildAssetBundle(BuildTarget.Android);
        }
        [MenuItem("Assets/AssetBundle/打包资源iOS")]
        public static void BuildAssetBundleForiOS()
        {
            BuildAssetBundle(BuildTarget.iOS);
        }

        [MenuItem("Assets/AssetBundle/打包资源", true)]
        [MenuItem("Assets/AssetBundle/打包资源Windows", true)]
        [MenuItem("Assets/AssetBundle/打包资源Android", true)]
        [MenuItem("Assets/AssetBundle/打包资源iOS", true)]
        public static bool AssetsSelectionTest()
        {
            return Selection.assetGUIDs.Length > 0;
        }
    }
}